import router from "@/router";
import axios from "axios";
import { Message } from "element-ui";
axios.defaults.timeout = 10000;
axios.defaults.baseURL = '/api'
axios.defaults.validateStatus = (status) => {
    return status >= 200 && status <= 600;
};

axios.defaults.withCredentials = true;
axios.interceptors.request.use(config => {
    config.withCredentials = true;
    return config;
}, err => {
    return Promise.reject(err);
})
axios.interceptors.response.use(res => {
    if (res.data.code === 0 && res.data.msg === 'NOTLOGIN') {// 返回登录页面
        console.log('---/backend/page/login---')
        localStorage.removeItem('userInfo')
        router.push('/login');
    } else if (res.data.code === 0) {
        Message.error(res.data.msg);
    } else {
        return res
    }
}, err => {
    return Promise.reject(new Error(err));
});
export default axios;